package org.tulkas.util;

import java.security.MessageDigest;
import java.util.UUID;

/**
 * Utility class to handle encoding related operations
 * 
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public abstract class EncodeUtil {
	/**
	 * MD5 encode.
	 * 
	 * @param content, source string
	 * @return the MD5 encode of the content.
	 */
	public static String md5(String content) {
		if (content == null)
			return null;
		byte[] unencodedPassword = content.getBytes();

		MessageDigest md = null;

		try {
			md = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			return content;
		}
		md.reset();
		md.update(unencodedPassword);
		byte[] encodedPassword = md.digest();
		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 0xff) < 0x10) {
				buf.append("0");
			}
			buf.append(Long.toString(encodedPassword[i] & 0xff, 16));
		}

		return buf.toString();
	}

	public static String uuid() {
		return UUID.randomUUID().toString();
	}

}
